<?php

require_once($path.'/db/sqlbloodlibrary2.php');

//limpia la salida de carácteres extraños
function tr($strIn){
	if(!check_utf8($strIn)){
		$utf8 = utf8_encode($strIn);
		//$str = htmlentities($strIn, ENT_QUOTES, 'ISO-8859-1', false);
	} else $utf8 = $strIn;
	return htmlentities($utf8, ENT_QUOTES, 'UTF-8', false);
}

//functión de traducción
function t($strIn){
	if(!check_utf8($strIn)){
		$utf8 = utf8_encode($strIn);
		//$str = htmlentities($strIn, ENT_QUOTES, 'ISO-8859-1', false);
	} else $utf8 = $strIn;
	return htmlentities($utf8, ENT_QUOTES, 'UTF-8', false);
}

function to_utf8($strIn){
	if(!check_utf8($strIn)){
		$utf8 = utf8_encode($strIn);
		return $utf8;
	}
	return $strIn;
}

function check_utf8($str) {
    $len = strlen($str);
    for($i = 0; $i < $len; $i++){
        $c = ord($str[$i]);
        if ($c > 128) {
            if (($c > 247)) return false;
            elseif ($c > 239) $bytes = 4;
            elseif ($c > 223) $bytes = 3;
            elseif ($c > 191) $bytes = 2;
            else return false;
            if (($i + $bytes) > $len) return false;
            while ($bytes > 1) {
                $i++;
                $b = ord($str[$i]);
                if ($b < 128 || $b > 191) return false;
                $bytes--;
            }
        }
    }
    return true;
} // end of check_utf8 

//codifica un comentario
function codeComment($texto){
	$texto = nl2br(htmlentities($texto, ENT_QUOTES, 'UTF-8'));
	$codigo = array(
		'/\\*\\*(.*?)\\*\\*/is', //**texto** --> <strong>texto</strong>
		'/\s\s+/', // '    ' -> ' ' (los espacios -extra- los quita!)
		'/(\\<br\s+\\/\\>\s?){2,}/is'
	);
	$reemplazo = array(
		'<strong>$1</strong>',
		' ',
		'<br /> <br /> '
	);
	$texto = preg_replace($codigo, $reemplazo, $texto);
	return $texto;
}

//Buscador de palabras 2.0
function eregi2($palabra, $str){
	$palabra = '#'.$palabra.'#i';
	if(preg_match($palabra, $str)) return TRUE;
	else return FALSE;
}

//para debuggear arrays
function prePrint($var){
	echo "<pre>";
	print_r($var);
	echo "</pre>";
}

//Traducción string de formulario
function formsql($consulta){
	global $conexion;
	return $conexion->formsql($consulta);
}

//Imprime una URL
function url($cadena) {
	global $path;
	global $dominio;
	
	//if (eregi($path, $cadena)) {
	if (stristr($cadena, $path) != false) {
		$tampath = strlen($path);
		$tam = strlen($cadena);
		for ($i = $tampath; $i < $tam; $i++){
			$aux .= $cadena[$i];
		}
		return $dominio . $aux;
	}
	return $dominio . $cadena;
}

//url relativa a dom/
function shortUrl($cadena){
	global $path;
	
	if (stristr($cadena, $path) != false) {
		$tampath = strlen($path);
		$tam = strlen($cadena);
		for ($i = $tampath; $i < $tam; $i++){
			$aux .= $cadena[$i];
		}
		return $aux;
	} else return $cadena;
}

//Url $absoluta a la raiz del servidor
function fullUrl($cadena){
global $path;
	$cadena = shortUrl($cadena);
	return $path.$cadena;
}

//Devuelve el dominio configurado
function printDominio(){
	global $dominio;
	return $dominio;
}

function is_email( $email, $check_dns = false ) {
	//By Wordpress 2.8.4
	// Test for the minimum length the email can be
	if ( strlen( $email ) < 3 ) return false;
	// Test for an @ character after the first position
	if ( strpos( $email, '@', 1 ) === false ) return false;
	// Split out the local and domain parts
	list( $local, $domain ) = explode( '@', $email, 2 );
	// LOCAL PART
	// Test for invalid characters
	if ( !preg_match( '/^[a-zA-Z0-9!#$%&\'*+\/=?^_`{|}~\.-]+$/', $local ) ) return false;
	// DOMAIN PART
	// Test for sequences of periods
	if ( preg_match( '/\.{2,}/', $domain ) ) return false;
	// Test for leading and trailing periods and whitespace
	if ( trim( $domain, " \t\n\r\0\x0B." ) !== $domain ) return false;
	// Split the domain into subs
	$subs = explode( '.', $domain );
	// Assume the domain will have at least two subs
	if ( 2 > count( $subs ) ) return false;
	// Loop through each sub
	foreach ( $subs as $sub ) {
		// Test for leading and trailing hyphens and whitespace
		if ( trim( $sub, " \t\n\r\0\x0B-" ) !== $sub ) return false;
		// Test for invalid characters
		if ( !preg_match('/^[a-z0-9-]+$/i', $sub ) ) return false;
	}
	// DNS
	// Check the domain has a valid MX and A resource record
	if ( $check_dns && function_exists( 'checkdnsrr' ) && !( checkdnsrr( $domain . '.', 'MX' ) || checkdnsrr( $domain . '.', 'A' ) ) ) return false;
	// Congratulations your email made it!
	return true;
}

function image_type_or_mime_type_to_extension($image_type, $include_dot = true) {
            define ("INVALID_IMAGETYPE", '');
           
            $extension = INVALID_IMAGETYPE;            /// Default return value for invalid input

            $image_type_identifiers = array (                                                                ### These values correspond to the IMAGETYPE constants
                    array (IMAGETYPE_GIF         => 'gif',     "mime_type" => 'image/gif'),                        ###  1 = GIF
                    array (IMAGETYPE_JPEG        => 'jpg',     "mime_type" => 'image/jpeg'),                        ###  2 = JPG
                    array (IMAGETYPE_PNG        => 'png',     "mime_type" => 'image/png'),                        ###  3 = PNG
                    array (IMAGETYPE_SWF        => 'swf',     "mime_type" => 'application/x-shockwave-flash'),    ###  4 = SWF  // A. Duplicated MIME type
                    array (IMAGETYPE_PSD        => 'psd',     "mime_type" => 'image/psd'),                        ###  5 = PSD
                    array (IMAGETYPE_BMP        => 'bmp',     "mime_type" => 'image/bmp'),                        ###  6 = BMP
                    array (IMAGETYPE_TIFF_II    => 'tiff',     "mime_type" => 'image/tiff'),                        ###  7 = TIFF (intel byte order)
                    array (IMAGETYPE_TIFF_MM    => 'tiff',     "mime_type" => 'image/tiff'),                        ###  8 = TIFF (motorola byte order)
                    array (IMAGETYPE_JPC        => 'jpc',     "mime_type" => 'application/octet-stream'),            ###  9 = JPC  // B. Duplicated MIME type
                    array (IMAGETYPE_JP2        => 'jp2',     "mime_type" => 'image/jp2'),                        ### 10 = JP2
                    array (IMAGETYPE_JPX        => 'jpf',     "mime_type" => 'application/octet-stream'),            ### 11 = JPX  // B. Duplicated MIME type
                    array (IMAGETYPE_JB2        => 'jb2',     "mime_type" => 'application/octet-stream'),            ### 12 = JB2  // B. Duplicated MIME type           
                    array (IMAGETYPE_SWC        => 'swc',     "mime_type" => 'application/x-shockwave-flash'),    ### 13 = SWC  // A. Duplicated MIME type
                    array (IMAGETYPE_IFF        => 'aiff',     "mime_type" => 'image/iff'),                        ### 14 = IFF
                    array (IMAGETYPE_WBMP        => 'wbmp',     "mime_type" => 'image/vnd.wap.wbmp'),                ### 15 = WBMP
                    array (IMAGETYPE_XBM        => 'xbm',     "mime_type" => 'image/xbm')                            ### 16 = XBM
            );                                                                                   
           
            if((is_int($image_type)) AND (IMAGETYPE_GIF <= $image_type) AND (IMAGETYPE_XBM >= $image_type)){
                $extension = $image_type_identifiers[$image_type-1]; // -1 because $image_type_identifiers array starts at [0]
                $extension = $extension[$image_type];
            }
            elseif(is_string($image_type) AND (($image_type != 'application/x-shockwave-flash') OR ($image_type != 'application/octet-stream'))){               
           
                $extension =  match_mime_type_to_extension($image_type, $image_type_identifiers);
            }
            else
            {
                $extension = INVALID_IMAGETYPE;
            }

               if(is_bool($include_dot)){

                   if((false != $include_dot) AND (INVALID_IMAGETYPE != $extension)){
                       $extension = '.' . $extension;
                   }
               }
               else
               {
                   $extension = INVALID_IMAGETYPE;
               }                 
      
           return $extension;

}

function match_mime_type_to_extension($image_type, $image_type_identifiers){
        // Return from loop on a match
        foreach($image_type_identifiers as $_key_outer_loop => $_val_outer_loop){           
            foreach($_val_outer_loop as $_key => $_val){
                if(is_int ($_key)){             // Keep record of extension for mime check
                    $extension = $_val;
                }
                if($_key == 'mime_type'){   
                    if($_val === $image_type){    // Found match no need to continue looping
                        return $extension;        ### Return
                    }                    
                }
            }
        }
        // Compared all values without match
        return $extension = INVALID_IMAGETYPE;   
}


?>